<?php
namespace App\Admin\Controllers\Basics;

use App\Models\Basics\AmazonAccount;
use App\Models\Basics\AmazonAccountCountry;
use App\Models\Basics\SysCountry;
use App\Models\Operation\ProfitSku;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Illuminate\Support\Facades\Request;


class AmazonAccountCountryController extends AdminController{

    protected $title = '亚马逊账号国家vat';

    protected $filters = [
        'account_id' => null,
        'country_id' => null,
    ];

    protected function grid()
    {
        $filtersField = array_keys($this->filters);
        $filters = $this->_getFilters($filtersField);
        $this->_initFilters($filters);
        $query = $this->_initQuery($filters);
        $paginator = $this->_paginator($query);
        $action = getAction($filtersField);
        $filtersBlade = [];
        $filtersBlade['account_id'] = getOptions(AmazonAccount::options(), $this->filters['account_id']);
        $filtersBlade['country_id'] = getOptions(SysCountry::options(), $this->filters['country_id']);

        return view('basics.amazon-account-country.index', compact('paginator','action','filtersBlade'));
    }

    private function _getFilters($filtersField){
        $filters = [];
        foreach ($filtersField as $field){
            $filters[$field] = request($field);
        }
        return $filters;
    }

    private function _initFilters($filters){
        foreach ($filters as $field => $filter){
            $this->filters[$field] = $filter;
        }
    }

    private function _initQuery($filters){

        $query = AmazonAccountCountry::query()
            ->groupBy('account_id')
            ->orderBy('account_id', 'DESC');

        foreach ($filters as $field => $filter){

            if($field == 'account_id'){
                if (isset($filter)) {
                    $query->where('account_id',$filter);
                }
            }
            if($field == 'country_id'){
                if (isset($filter)) {
                    $query->where('country_id',$filter);
                }
            }
            if(!$filter)continue;
        }

        return $query;
    }

    private function _paginator($query){
        return $query->paginate((int) (request("per_page",10)))->appends(Request::input());
    }

    public function update($id){
        try{
            $name = Request::get('name');
            $value = Request::get('value');
            if($name == 'vat' && $value < 0)return response()->json(['status' => false, 'message' => 'vat必须为数字!']);
            $model = AmazonAccountCountry::query()->find($id);
            $model->{$name} = $value;
            if(!$model->save()){
                return response()->json(['status' => false, 'message' => '更新失败!']);
            }else{
                return response()->json(['status' => true, 'message' => '更新成功!']);
            }
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常!']);
        }
    }

    /* protected function grid(){
            $grid = new Grid(new AmazonAccountCountry());
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('account.account','账号');
            $grid->column('country.country','国家');
            $grid->column('account.is_local','是否本土账号')->using(AmazonAccount::$_isLocal)->label([1 => 'info', 0 => 'default']);
            $grid->column('vat','VAT')->editable()->display(function ($value){
                return $value.'%';
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
                $actions->disableDelete();
            });
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disableExport();
            $grid->filter(function($filter){
                $filter->column(1/3, function ($filter) {
                    $filter->equal('account_id','账号')->select(AmazonAccount::options());
                });
                $filter->column(1/3, function ($filter) {
                    $filter->equal('country_id', '国家')->select(AmazonCountry::options());
                });
            });
            return $grid;
        }
    */

    public function form()
    {
        $form = new Form(new AmazonAccountCountry());
        $form->hidden('id');
        $form->select('account_id','区域')->options(AmazonAccount::options())->setWidth(5)->readonly();
        $form->select('country_id', '国家')->options(SysCountry::options())->setWidth(5)->readonly();
        $form->rate('vat','VAT比例')->default('0.000')->required()->setWidth(5);
        $form->saving(function (Form $form) {
            if ($form->vat && $form->model()->vat != $form->vat) {
                ProfitSku::query()
                    //->where('account_id',$form->model()->account_id)
                    ->where('country_id',$form->model()->country_id)
                    ->update(['vat' => $form->vat]);
            }
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });
        return $form;
    }
}
